﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using MemberShipDAL.DataObject;
using MemberServerBLL;
using System.Collections;

/// <summary>
/// Asp.net RBAC Member Framework的核心类，所有需要权限控制和用户管理的页面都需要继承此页面
/// </summary>
public class PageBase : System.Web.UI.Page
{
    public PageBase()
    {

    }

    private Guid currentUserId;

    public Guid CurrentUserId
    {
        get { return currentUserId; }
        set { currentUserId = value; }
    }

    /// <summary>
    /// 获得用户详细信息
    /// </summary>
    /// <returns></returns>
    static public DataUserDetail.wmf_UserDetailRow UserDetail()
    {
        string userId = Membership.GetUser().ProviderUserKey.ToString();
        if (userId == string.Empty)
        {
            throw new Exception("用户尚未登陆");
        }
        else
        {
            DataUserDetail.wmf_UserDetailRow dr = new DataUserDetail.wmf_UserDetailDataTable().Newwmf_UserDetailRow();
            BLL_UserDetail userDetailBll = new BLL_UserDetail();
            dr = userDetailBll.GetUserDetail(new Guid(userId));
            return dr;
        }
    }

    /// <summary>
    /// 获取当前用户的权限
    /// </summary>
    /// <param name="privilegeId">权限ID</param>
    /// <returns></returns>
    static public bool GetPrivilege(string privilegeId)
    {
        MembershipUser u = Membership.GetUser(HttpContext.Current.User.Identity.Name, true);

        return GetPrivilege(new Guid(u.ProviderUserKey.ToString()), privilegeId);
    }
    /// <summary>
    /// 获取指定用户的权限
    /// </summary>
    /// <param name="userId">用户Id</param>
    /// <param name="privilegeId">权限ID</param>
    /// <returns></returns>
    static public bool GetPrivilege(Guid userId, string privilegeId)
    {
        return GetPrivilege(userId, privilegeId, null);
    }

    /// <summary>
    /// 获取当前用户的权限
    /// </summary>
    /// <param name="resourceId">资源Id</param>
    /// <param name="operateId">操作Id</param>
    /// <returns></returns>
    static public bool GetPrivilege(string resourceId, int operateId)
    {
        //获取当前用户
        MembershipUser u = Membership.GetUser(HttpContext.Current.User.Identity.Name, true);
        return GetPrivilege(u.ProviderUserKey.ToString(), resourceId, operateId);
    }


    /// <summary>
    /// 获取指定用户的权限
    /// </summary>
    /// <param name="userId">用户Id</param>
    /// <param name="resourceId">资源Id</param>
    /// <param name="operateId">操作Id</param>
    /// <returns></returns>
    static public bool GetPrivilege(string userId, string resourceId, int operateId)
    {
        string privilegeId = GetPrivilegeID(resourceId, operateId);

        return GetPrivilege(new Guid(userId), privilegeId, null);
    }

    /// <summary>
    /// 根据业务参数和用户Id获得用户是否有权向操作此业务点
    /// 注:业务参数功能尚未实现
    /// </summary>
    /// <param name="userId"></param>
    /// <param name="privilegeId"></param>
    /// <param name="bizParameter"></param>
    /// <returns></returns>
    static public bool GetPrivilege(Guid userId, string privilegeId, string[] bizParameter)
    {
        //获得privilegeId所拥有的Role
        ArrayList roles = BLL_PrivilegeInRoles.GetRolesID(new Guid(privilegeId));
        foreach (object role in roles)
        {
            //如果用户在这个用户组中，那么这个操作可以进行
            if (HttpContext.Current.User.IsInRole(new BLL_AspNet_Roles().GerRoleNameById(role.ToString())))
                return true;
        }
        return false;
    }

    /// <summary>
    /// 通过ResourceId和OperateId获得PrivilegeID
    /// </summary>
    /// <param name="resourceId"></param>
    /// <param name="operateId"></param>
    /// <returns></returns>
    static public string GetPrivilegeID(string resourceId, int operateId)
    {
        string privilegeId = new BLL_Privilege().GetPrivilegeId(new Guid(resourceId), operateId);
        return privilegeId;
    }


    /// <summary>
    /// 获得配置值,（v1.0版本中未实现）
    /// </summary>
    /// <param name="userId"></param>
    /// <param name="privilegeId"></param>
    /// <returns></returns>
    private object GetPrivilegeValue(string userId, string privilegeId)
    {
        return GetPrivilegeValue(userId, privilegeId, null, null);
    }

    /// <summary>
    /// 获得配置值（v1.0版本中带业务参数,未实现）
    /// </summary>
    /// <param name="userId"></param>
    /// <param name="privilegeId"></param>
    /// <param name="bizParameter"></param>
    /// <returns></returns>
    private object GetPrivilegeValue(string userId, string privilegeId, string roleId, string[] bizParameter)
    {
        throw new Exception("The method or operation is not implemented.");
    }


}
